#include "solve.h"

int solve::solve_guass(double vTmp[][101],double ansTmp[],int n)
{
    try{
            for(int i=0;i<n;i++){
                int tmp = i;
                //showMatrix(vTmp,n);
                for(int j=i+1;j<n;j++){
                    if(fabs(vTmp[j][i])>fabs(vTmp[tmp][i])){
                        tmp = j;
                    }
                }

                if(tmp != i){
                    for(int j=i;j<=n;j++){

                        swap(vTmp[tmp][j],vTmp[i][j]);
                    }
                }
               // showMatrix(vTmp,n);
                if(vTmp[i][i]==0){
                    return 2;
                }
                double dtmp = vTmp[i][i];
                for(int j=i;j<=n;j++){
                    vTmp[i][j]=vTmp[i][j]/dtmp;
                }
                for(int j=i+1;j<n;j++){
                    double dtmp = vTmp[j][i];
                    for(int k=i;k<=n;k++){
                        vTmp[j][k]=vTmp[j][k]-dtmp*vTmp[i][k];
                    }
                    vTmp[i][j] = vTmp[i][j]/vTmp[i][i];
                }
            }
            for(int i=n-1;i>=0;i--){
                double tmp = 0;
                for(int j=n-1;j>i;j--){
                    tmp +=vTmp[i][j]*ansTmp[j];
                }
                ansTmp[i]=vTmp[i][n]-tmp;
            }
            return 1;
        }catch(...){
            return -1;
        }
}
int solve::solve_doolittle(double vTmp[][101], double vans[][101], int n){
    try{

        return 1;
    }catch(...){
        return -1;
    }
}
